package com.qyhl.module_practice.order.mine.detail;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.glide.GlideRoundTransform;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.Image;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeOrderBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.commonlib.utils.view.NineGridLayout.NineAdapter;
import com.qyhl.webtv.commonlib.utils.view.NineGridLayout.NineLayout;
import com.qyhl.webtv.commonlib.utils.view.imagebrowser.MNImageBrowser;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_ORDER_DETAIL)
public class PracticeOrderDetailActivity extends BaseActivity {
    @BindView(R2.id.content)
    TextView content;
    @BindView(R2.id.nine_layout)
    NineLayout nineLayout;
    @BindView(R2.id.address)
    TextView address;
    @BindView(R2.id.type_name)
    TextView typeName;
    @BindView(R2.id.status)
    ImageView status;
    @BindView(R2.id.act_title)
    TextView actTitle;
    @BindView(R2.id.act_layout)
    RelativeLayout actLayout;

    private PracticeOrderBean mData;

    private String instId;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_order_detail;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mData = (PracticeOrderBean) getIntent().getSerializableExtra("bean");
        instId = getIntent().getStringExtra("instId");
        initView();
    }

    @Override
    protected void setListener() {

    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        content.setText(mData.getDetail());

        address.setText("地址：" + mData.getAddress());
        typeName.setText(mData.getParentTypeName() + "·" + mData.getTypeName());

        switch (mData.getStatus()) {
            case 0:
                status.setImageResource(R.drawable.practice_order_wait_icon);
                break;
            case 1:
                status.setImageResource(R.drawable.practice_order_refuse_icon);
                break;
            case 2:
                status.setImageResource(R.drawable.practice_order_going_icon);
                break;
            case 3:
                status.setImageResource(R.drawable.practice_order_ready_icon);
                break;
            default:
                status.setImageResource(R.drawable.practice_order_going_icon);
                break;
        }

        if (mData.getActivity() == null || mData.getActivity().getId() == 0) {
            actLayout.setVisibility(View.GONE);
        } else {
            actLayout.setVisibility(View.VISIBLE);
            actTitle.setText(mData.getActivity().getName());
        }


        if (mData.getImages() != null && mData.getImages().size() > 0) {
            nineLayout.setVisibility(View.VISIBLE);
            nineLayout.setGap(10);
            List<Image> imageList = initDatas(mData.getImages());
            Adapter adapter = new Adapter(PracticeOrderDetailActivity.this, imageList);
            nineLayout.setAdapter(adapter);
            nineLayout.setOnItemClickListerner(new NineLayout.OnItemClickListerner() {
                @Override
                public void onItemClick(View view, int position) {
                    ArrayList<String> mList = new ArrayList<>();
                    for (int i = 0; i < mData.getImages().size(); i++) {
                        mList.add(mData.getImages().get(i).getUrl());
                    }
                    MNImageBrowser.showImageBrowser(PracticeOrderDetailActivity.this, view, position, mList);
                }
            });
        } else {
            nineLayout.setVisibility(View.GONE);
        }

    }


    @OnClick({R2.id.back_btn, R2.id.more})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.back_btn) {
            finish();
        } else if (id == R.id.more) {
            String userName = CommonUtils.getInstance().getUserName();
            Bundle bundle = new Bundle();
            bundle.putString("id", mData.getActivity().getId() + "");
            bundle.putString("userName", userName);
            bundle.putString("title", mData.getActivity().getName());
            bundle.putString("instId",instId);
            RouterManager.router(ARouterPathConstant.PRACTICE_ACT_DETAIL,bundle);

        }
    }


    class Adapter extends NineAdapter {

        public Adapter(Context context, List<Image> list) {
            super(context, list);
        }

        @Override
        public int getCount() {
            return (list == null) ? 0 : list.size();
        }

        @Override
        public String getUrl(int position) {
            return getItem(position) == null ? null : ((Image) getItem(position)).getUrl();
        }

        @Override
        public Object getItem(int position) {
            return (list == null) ? null : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View view) {
            ImageView iv = new ImageView(PracticeOrderDetailActivity.this);
            Glide.with(PracticeOrderDetailActivity.this)
                    .load(list.get(i).getUrl())
                    .apply(new RequestOptions().error(R.drawable.cover_normal_default).centerCrop().optionalTransform(new GlideRoundTransform(4)))
                    .into(iv);
            return iv;
        }
    }

    //生成九宫格模型
    private List<Image> initDatas(List<PracticeOrderBean.Image> beanList) {
        List<Image> images = new ArrayList<>();
        for (int i = 0; i < beanList.size(); i++) {
            images.add(new Image(beanList.get(i).getUrl(), 0, 0));
        }
        return images;
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_ORDER_DETAIL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_ORDER_DETAIL);
    }

}
